import 'package:flutter/material.dart';
import 'package:flutter_learning/utils/ly_app_color.dart';
import 'package:fl_chart/fl_chart.dart';
import 'indicator.dart';

class LYPieChartDataItem {
  final String? name;
  final num? value;
  final bool isInt;
  const LYPieChartDataItem({this.name, this.value, this.isInt = false});
}

class LYPieChartView extends StatefulWidget {
  final String title;
  final String? content;
  final String? centerTitle;
  final String? centerContent;
  final bool titleVisible;
  final List<LYPieChartDataItem> dataItems;
  final double? aspectRatio;
  final double radius;
  const LYPieChartView({
    Key? key,
    required this.title,
    this.content,
    this.centerTitle,
    this.centerContent,
    required this.dataItems,
    this.titleVisible = true,
    this.aspectRatio,
    this.radius = 75,
  }) : super(key: key);

  @override
  State<LYPieChartView> createState() => _LYPieChartViewState();
}

class _LYPieChartViewState extends State<LYPieChartView> {
  int touchedIndex = -1;
  late List<Color> _colors;
  List<PieChartSectionData> _sectionDataItems = [];
  List<Widget> _indicatorItems = [];

  @override
  void initState() {
    super.initState();
    _colors = [
      const Color(0xff8cb2ff),
      const Color(0xff90e9c6),
      const Color(0xff96a6c2),
      const Color(0xfff4dc97),
      const Color(0xffbae290),
      const Color(0xff13d38e),
      const Color(0xff845bef),
      const Color(0xfff8b250),
    ];
  }

  @override
  Widget build(BuildContext context) {
    _makeDatas();
    final aspectRatio = widget.aspectRatio;
    if (aspectRatio != null) {
      return AspectRatio(
        aspectRatio: aspectRatio,
        child: _makeContentView(),
      );
    } else {
      return _makeContentView();
    }
  }

  Widget _makeContentView() {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: AppColor.whiteColor,
      ),
      clipBehavior: Clip.hardEdge,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          if (widget.titleVisible)
            Container(
              height: 30,
              color: AppColor.whiteColor,
              padding: const EdgeInsets.only(left: 15, right: 15),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    widget.title,
                    style: const TextStyle(
                        fontSize: 13, color: AppColor.color333333, fontWeight: FontWeight.bold),
                  ),
                  Text(
                    widget.content ?? '',
                    style: const TextStyle(
                        fontSize: 13, color: AppColor.color333333, fontWeight: FontWeight.bold),
                  )
                ],
              ),
            ),
          Expanded(child: _makePieView()),
        ],
      ),
    );
  }

  Widget _makePieView() {
    return Padding(
      padding: const EdgeInsets.all(15),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            color: Colors.white,
            width: widget.radius * 2,
            height: widget.radius * 2,
            child: PieChart(
              PieChartData(
                  borderData: FlBorderData(
                    show: false,
                  ),
                  sectionsSpace: 0,
                  centerSpaceRadius: 0,
                  sections: _sectionDataItems),
            ),
          ),
          const SizedBox(
            width: 15,
          ),
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: _indicatorItems,
              ),
            ),
          ),
        ],
      ),
    );
  }

  void _makeDatas() {
    List<PieChartSectionData> chartDatas = [];
    List<Widget> indicatorItems = [];
    final count = widget.dataItems.length;
    widget.dataItems.asMap().forEach((index, model) {
      Color color;
      if (_colors.length > count) {
        color = _colors[index];
      } else {
        color = randomColor();
        _colors.add(color);
      }
      final value = model.value?.abs();
      final sectionData = PieChartSectionData(
        color: color,
        value: value?.toDouble() ?? 0,
        radius: widget.radius,
        showTitle: false,
        titleStyle:
            const TextStyle(fontSize: 15, fontWeight: FontWeight.bold, color: AppColor.whiteColor),
      );
      chartDatas.add(sectionData);
      final indicatorItem = Indicator(
        color: color,
        text: model.name ?? '',
        content: model.value?.toStringAsFixed(model.isInt ? 0 : 2),
      );
      indicatorItems.add(indicatorItem);
    });
    _sectionDataItems = chartDatas;
    _indicatorItems = indicatorItems;
  }
}
